package com.demostic.demostic_test.utils;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.demostic.demostic_test.R;

import java.util.HashMap;

public class DisplayUtil {
    private static TextView textView;

    public static int px2dip(Context context, float pxValue) {
        return (int) ((pxValue / context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    public static int dip2px(Context context, float dipValue) {
        return (int) ((dipValue * context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        return (int) ((pxValue / context.getResources().getDisplayMetrics().scaledDensity) + 0.5f);
    }

    public static int sp2px(Context context, float spValue) {
        return (int) ((spValue * context.getResources().getDisplayMetrics().scaledDensity) + 0.5f);
    }

    public static TextView setTextCount(Context context, RelativeLayout relativeLayout) {
        textView = new TextView(context);
        textView.setTextColor(ViewCompat.MEASURED_STATE_MASK);
        textView.setGravity(17);
        textView.setBackground(ContextCompat.getDrawable(context, R.drawable.text_style));
        textView.setTextSize(15.0f);
        textView.setAlpha(0.8f);
        RelativeLayout.LayoutParams lp_tv = new RelativeLayout.LayoutParams(dip2px(context, 80.0f), dip2px(context, 30.0f));
        relativeLayout.addView(textView);
        lp_tv.addRule(9);
        lp_tv.addRule(10);
        lp_tv.topMargin = dip2px(context, 20.0f);
        lp_tv.leftMargin = dip2px(context, 25.0f);
        textView.setLayoutParams(lp_tv);
        return textView;
    }

    public static void setBatteryImage(int batteryCap, ImageView batteryImage) {
        if (batteryCap <= 6) {
            batteryImage.setImageResource(R.drawable.map_aty_battery1);
        } else if (batteryCap < 35) {
            batteryImage.setImageResource(R.drawable.map_aty_battery2);
        } else if (batteryCap < 75) {
            batteryImage.setImageResource(R.drawable.map_aty_battery3);
        } else if (batteryCap >= 75) {
            batteryImage.setImageResource(R.drawable.map_aty_battery4);
        }
    }

    public static void setGifShow(Context context, Integer resourceId, ImageView imageView) {
        Glide.with(context).load(resourceId).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
    }

    public static ImageView initRightZoom(Context context, RelativeLayout relativeLayout, int id) {
        ImageView imageView = new ImageView(context);
        imageView.setId(id);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(dip2px(context, 30.0f), dip2px(context, 30.0f));
        relativeLayout.addView(imageView);
        lp.addRule(11);
        if (id == R.id.image_battery) {
            lp.addRule(12);
        } else if (id == R.id.image_clock) {
            lp.addRule(2, R.id.image_battery);
        } else if (id == 2131755028) {
            lp.addRule(2, R.id.image_clock);
        }
        lp.bottomMargin = dip2px(context, 20.0f);
        lp.rightMargin = dip2px(context, 25.0f);
        imageView.setLayoutParams(lp);
        return imageView;
    }

    public static ImageView initLeftZoom(Context context, RelativeLayout relativeLayout, int id, int resId) {
        ImageView imageView = new ImageView(context);
        imageView.setId(id);
        imageView.setImageResource(resId);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(dip2px(context, 30.0f), dip2px(context, 30.0f));
        relativeLayout.addView(imageView);
        lp.addRule(9);
        if (id == 2131755021) {
            lp.addRule(12);
        } else if (id == 2131755018) {
            lp.addRule(2, 2131755021);
        }
        lp.bottomMargin = dip2px(context, 20.0f);
        lp.leftMargin = dip2px(context, 25.0f);
        imageView.setLayoutParams(lp);
        return imageView;
    }

    public static ImageView initBottomZoom(Context context, RelativeLayout relativeLayout, int id, int resId) {
        ImageView imageView = new ImageView(context);
        imageView.setId(id);
        imageView.setImageResource(resId);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(dip2px(context, 30.0f), dip2px(context, 30.0f));
        relativeLayout.addView(imageView);
        lp.addRule(12);
        lp.bottomMargin = dip2px(context, 20.0f);
        if (id == 2131755026) {
            lp.addRule(0, R.id.image_battery);
            lp.rightMargin = dip2px(context, 40.0f);
        } else if (id == 2131755023) {
            lp.addRule(0,2131755026);
            lp.rightMargin = dip2px(context, 15.0f);
        }
        imageView.setLayoutParams(lp);
        return imageView;
    }

    public static Paint getMapPaint(int color) {
        Paint paint = new Paint();
        paint.setColor(color);
        return paint;
    }

    public static Paint getRoadPaint(int color) {
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        return paint;
    }

    public static Paint getHostPaint(int color) {
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setStrokeWidth(1.0f);
        paint.setAntiAlias(true);
        return paint;
    }

    public static Paint getFbdPaint() {
        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#7fef7e1c"));
        paint.setStrokeWidth(1.0f);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        return paint;
    }

    public static Paint getFbdPaints() {
        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#7ffff000"));
        paint.setStrokeWidth(1.0f);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        return paint;
    }

    public static Paint getWhgPaint() {
        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#7fe40082"));
        paint.setStrokeWidth(1.0f);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        return paint;
    }

    public static Paint getFbdStrokePaint(int color) {
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setStrokeWidth(1.0f);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setDither(true);
        return paint;
    }

    public static void setApGuideStep(ImageView iv1, ImageView iv2, Drawable drb1, Drawable drb2) {
        iv1.setImageDrawable(drb1);
        iv2.setImageDrawable(drb2);
    }

    public static HashMap<Integer, Paint> getdrPainthm(Context context) {
        HashMap<Integer, Paint> hm = new HashMap<>();
        Paint pt_1 = getMapPaint(context.getResources().getColor(R.color.pt_1));
        Paint pt_2 = getMapPaint(context.getResources().getColor(R.color.pt_2));
        Paint pt_3 = getMapPaint(context.getResources().getColor(R.color.pt_3));
        Paint pt_4 = getMapPaint(context.getResources().getColor(R.color.pt_4));
        Paint pt_5 = getMapPaint(context.getResources().getColor(R.color.pt_5));
        Paint pt_6 = getMapPaint(context.getResources().getColor(R.color.pt_6));
        Paint pt_7 = getMapPaint(context.getResources().getColor(R.color.pt_7));
        Paint pt_8 = getMapPaint(context.getResources().getColor(R.color.pt_8));
        Paint pt_9 = getMapPaint(context.getResources().getColor(R.color.pt_9));
        Paint pt_10 = getMapPaint(context.getResources().getColor(R.color.pt_10));
        Paint pt_11 = getMapPaint(context.getResources().getColor(R.color.pt_11));
        Paint pt_12 = getMapPaint(context.getResources().getColor(R.color.pt_12));
        Paint pt_13 = getMapPaint(context.getResources().getColor(R.color.pt_13));
        Paint pt_14 = getMapPaint(context.getResources().getColor(R.color.pt_14));
        Paint pt_15 = getMapPaint(context.getResources().getColor(R.color.pt_15));
        Paint pt_16 = getMapPaint(context.getResources().getColor(R.color.pt_16));
        Paint pt_17 = getMapPaint(context.getResources().getColor(R.color.pt_17));
        Paint pt_18 = getMapPaint(context.getResources().getColor(R.color.pt_18));
        Paint pt_19 = getMapPaint(context.getResources().getColor(R.color.pt_19));
        Paint pt_20 = getMapPaint(context.getResources().getColor(R.color.pt_20));
        Paint pt_21 = getMapPaint(context.getResources().getColor(R.color.pt_21));
        Paint pt_22 = getMapPaint(context.getResources().getColor(R.color.pt_22));
        Paint pt_23 = getMapPaint(context.getResources().getColor(R.color.pt_23));
        Paint pt_24 = getMapPaint(context.getResources().getColor(R.color.pt_24));
        Paint pt_25 = getMapPaint(context.getResources().getColor(R.color.pt_25));
        Paint pt_26 = getMapPaint(context.getResources().getColor(R.color.pt_26));
        Paint pt_27 = getMapPaint(context.getResources().getColor(R.color.pt_27));
        Paint pt_28 = getMapPaint(context.getResources().getColor(R.color.pt_28));
        Paint pt_29 = getMapPaint(context.getResources().getColor(R.color.pt_29));
        Paint pt_30 = getMapPaint(context.getResources().getColor(R.color.pt_30));
        Paint pt_31 = getMapPaint(context.getResources().getColor(R.color.pt_31));
        Paint pt_32 = getMapPaint(context.getResources().getColor(R.color.pt_32));
        hm.put(1, pt_1);
        hm.put(2, pt_2);
        hm.put(3, pt_3);
        hm.put(4, pt_4);
        hm.put(5, pt_5);
        hm.put(6, pt_6);
        hm.put(7, pt_7);
        hm.put(8, pt_8);
        hm.put(9, pt_9);
        hm.put(10, pt_10);
        hm.put(11, pt_11);
        hm.put(12, pt_12);
        hm.put(13, pt_13);
        hm.put(14, pt_14);
        hm.put(15, pt_15);
        hm.put(16, pt_16);
        hm.put(17, pt_17);
        hm.put(18, pt_18);
        hm.put(19, pt_19);
        hm.put(20, pt_20);
        hm.put(21, pt_21);
        hm.put(22, pt_22);
        hm.put(23, pt_23);
        hm.put(24, pt_24);
        hm.put(25, pt_25);
        hm.put(26, pt_26);
        hm.put(27, pt_27);
        hm.put(28, pt_28);
        hm.put(29, pt_29);
        hm.put(30, pt_30);
        hm.put(31, pt_31);
        hm.put(32, pt_32);
        return hm;
    }

    public static HashMap<Integer, Paint> getPainthms(Context context) {
        HashMap<Integer, Paint> hms = new HashMap<>();
        Paint pt_1 = getMapPaint(context.getResources().getColor(R.color.pt_1s));
        Paint pt_2 = getMapPaint(context.getResources().getColor(R.color.pt_2s));
        Paint pt_3 = getMapPaint(context.getResources().getColor(R.color.pt_3s));
        Paint pt_4 = getMapPaint(context.getResources().getColor(R.color.pt_4s));
        Paint pt_5 = getMapPaint(context.getResources().getColor(R.color.pt_5s));
        Paint pt_6 = getMapPaint(context.getResources().getColor(R.color.pt_6s));
        Paint pt_7 = getMapPaint(context.getResources().getColor(R.color.pt_7s));
        Paint pt_8 = getMapPaint(context.getResources().getColor(R.color.pt_8s));
        Paint pt_9 = getMapPaint(context.getResources().getColor(R.color.pt_9s));
        Paint pt_10 = getMapPaint(context.getResources().getColor(R.color.pt_10s));
        Paint pt_11 = getMapPaint(context.getResources().getColor(R.color.pt_11s));
        Paint pt_12 = getMapPaint(context.getResources().getColor(R.color.pt_12s));
        Paint pt_13 = getMapPaint(context.getResources().getColor(R.color.pt_13s));
        Paint pt_14 = getMapPaint(context.getResources().getColor(R.color.pt_14s));
        Paint pt_15 = getMapPaint(context.getResources().getColor(R.color.pt_15s));
        Paint pt_16 = getMapPaint(context.getResources().getColor(R.color.pt_16s));
        Paint pt_17 = getMapPaint(context.getResources().getColor(R.color.pt_17s));
        Paint pt_18 = getMapPaint(context.getResources().getColor(R.color.pt_18s));
        Paint pt_19 = getMapPaint(context.getResources().getColor(R.color.pt_19s));
        Paint pt_20 = getMapPaint(context.getResources().getColor(R.color.pt_20s));
        Paint pt_21 = getMapPaint(context.getResources().getColor(R.color.pt_21s));
        Paint pt_22 = getMapPaint(context.getResources().getColor(R.color.pt_22s));
        Paint pt_23 = getMapPaint(context.getResources().getColor(R.color.pt_23s));
        Paint pt_24 = getMapPaint(context.getResources().getColor(R.color.pt_24s));
        Paint pt_25 = getMapPaint(context.getResources().getColor(R.color.pt_25s));
        Paint pt_26 = getMapPaint(context.getResources().getColor(R.color.pt_26s));
        Paint pt_27 = getMapPaint(context.getResources().getColor(R.color.pt_27s));
        Paint pt_28 = getMapPaint(context.getResources().getColor(R.color.pt_28s));
        Paint pt_29 = getMapPaint(context.getResources().getColor(R.color.pt_29s));
        Paint pt_30 = getMapPaint(context.getResources().getColor(R.color.pt_30s));
        Paint pt_31 = getMapPaint(context.getResources().getColor(R.color.pt_31s));
        Paint pt_32 = getMapPaint(context.getResources().getColor(R.color.pt_32s));
        hms.put(1, pt_1);
        hms.put(2, pt_2);
        hms.put(3, pt_3);
        hms.put(4, pt_4);
        hms.put(5, pt_5);
        hms.put(6, pt_6);
        hms.put(7, pt_7);
        hms.put(8, pt_8);
        hms.put(9, pt_9);
        hms.put(10, pt_10);
        hms.put(11, pt_11);
        hms.put(12, pt_12);
        hms.put(13, pt_13);
        hms.put(14, pt_14);
        hms.put(15, pt_15);
        hms.put(16, pt_16);
        hms.put(17, pt_17);
        hms.put(18, pt_18);
        hms.put(19, pt_19);
        hms.put(20, pt_20);
        hms.put(21, pt_21);
        hms.put(22, pt_22);
        hms.put(23, pt_23);
        hms.put(24, pt_24);
        hms.put(25, pt_25);
        hms.put(26, pt_26);
        hms.put(27, pt_27);
        hms.put(28, pt_28);
        hms.put(29, pt_29);
        hms.put(30, pt_30);
        hms.put(31, pt_31);
        hms.put(32, pt_32);
        return hms;
    }
}
